package com.i2works.smartluolong.api.controller.suggestion;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.i2works.smartluolong.api.utils.BaseModule;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.entity.Tree;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.common.TreeUtil;
import com.i2works.smartluolong.web.entity.org.OrgDO;
import com.i2works.smartluolong.web.entity.org.OrgQuery;
import com.i2works.smartluolong.web.entity.suggestion.SuggestionDO;
import com.i2works.smartluolong.web.entity.suggestion.SuggestionQuery;
import com.i2works.smartluolong.web.service.org.IOrgService;
import com.i2works.smartluolong.web.service.suggestion.ISuggestionService;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * 投诉及建议
 *
 * @author liu Yapeng
 * @date 2018/7/23 20:07
 */

@Controller
@RequestMapping("/suggestion")
public class SuggestionController extends BaseModule {

    private final ISuggestionService suggestionService;
    private final IOrgService orgService;

    public SuggestionController(ISuggestionService suggestionService, IOrgService orgService) {
        this.suggestionService = suggestionService;
        this.orgService = orgService;
    }


    /**
     * 无分页父类资源树
     * showStatus
     *
     * @param ：0显示所有的目录（不含按钮），1显示全部分类, 2显示所有正常父类
     */
    @PostMapping("/orgtree")
    @ResponseBody
    public Result queryTreeList() {
        List<Tree> result = Lists.newArrayList();
        try {
            OrgQuery query = new OrgQuery();
            query.setShowStatus(0);
            List<OrgDO> resList = orgService.queryTreeList(query);
            List<Tree> treeDataList = Lists.newArrayList();
            for (OrgDO org : resList) {
                if (null != this.orgTree(org) && org.getStatus() == 1) {
                    treeDataList.add(this.orgTree(org));
                }
            }
            result = TreeUtil.getNodeTree(0, treeDataList);
        } catch (Exception e) {
            logger.error("", e);
        }
        return R.success(result);
    }

    /**
     * 申请
     */
    @GetMapping("/apply/{type}")
    public Result apply(@PathVariable("type") Integer type) {
        JSONObject jsonObject = new JSONObject();
        try {
            OrgQuery query = new OrgQuery();
            query.setStatus(1);
            query.setLimit(500L);
            List<OrgDO> orgList = orgService.queryList(query);
            jsonObject.put("orgList", orgList);
            jsonObject.put("type", type);
            return R.success(jsonObject);
        } catch (Exception e) {
            logger.error("", e);
            return R.error();
        }
    }


    /**
     * 提交修改投诉建议
     */
    @PostMapping("/save")
    @ResponseBody
    public Result save(SuggestionDO suggestion) {
        try {
            suggestion.setCreateName(getUserName());
            suggestion.setUpdateName(getUserName());
            suggestion.setUserId(getUserId());
            suggestionService.saveOrUpdate(suggestion);
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.error();
        }
        return R.success();
    }


    /**
     * 个人投诉建议列表
     **/

    @PostMapping("/list")
    @ResponseBody
    private Result list(Integer userId, @ModelAttribute SuggestionQuery query) {
        try {
            query.setUserId(userId);
            query.setStatus(1);
            Integer count = suggestionService.queryCount(query);
            List<SuggestionDO> list = new ArrayList<SuggestionDO>();
            if (count != 0) {
                list = suggestionService.queryList(query);
            }
            list = transform(list);
            return R.success(list, count);
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.error();
        }

    }


    /**
     * 投诉建议详情
     **/

    @PostMapping("/view")
    @ResponseBody
    private Result view(Integer id) {
        try {
            SuggestionQuery query = new SuggestionQuery();
            query.setId(id);
            SuggestionDO suggestion = suggestionService.query(query);
            return R.success(suggestion);
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.error();
        }

    }
    /**
     * 返回部门名称
     * */
    private List<SuggestionDO> transform(List<SuggestionDO> list) {
        List<SuggestionDO> newList = new ArrayList<SuggestionDO>();
        OrgQuery query = new OrgQuery();
        if (list.size() != 0) {
            for (SuggestionDO suggestionDO : list) {
                OrgDO org = new OrgDO();
                try {
                    query.setId(suggestionDO.getOrgId());
                    org = orgService.query(query);
                    suggestionDO.setOrgName(org.getOrgName());
                    newList.add(suggestionDO);
                } catch (ServiceException e) {
                    e.printStackTrace();
                }
            }
        }

        return newList;
    }
    /**
     * 生成过滤后的树结构
     */
    private Tree orgTree(OrgDO org) throws Exception {
        Tree tree = new Tree();
        tree.setId(org.getId());
        tree.setPid(org.getPid());
        if (org.getStatus() == 0) {
            tree.setText(org.getOrgName() + "『禁』");
        } else {
            tree.setText(org.getOrgName());
        }
        tree.setType(org.getOrgType());
        tree.setSort(org.getSort());
        tree.setStatus(org.getStatus());
        tree.setUrl(org.getOrgCode());
        return tree;
    }


}
